package design.simdi.code.final_paper.utils;

import java.util.List;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NoRecordsToolbar;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.OddEvenItem;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;

/**
 * 
 * @author Marius Dumitriu - (Jun 18, 2010 - 5:28:38 PM)
 * 
 * @param <T>
 */
public abstract class MyDataTable<T> extends DataTable<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OddEvenItem<T> selectedRow;

	public MyDataTable(String id, IColumn<T>[] columns, IDataProvider<T> dataProvider, int rowsPerPage) {
		super(id, columns, dataProvider, rowsPerPage);
	}

	@SuppressWarnings("unchecked")
	public MyDataTable(String id, final List<IColumn<T>> columns, ISortableDataProvider<T> dataProvider, int rowsPerPage) {
		this(id, columns.toArray(new IColumn[columns.size()]), dataProvider, rowsPerPage);
	}

	public MyDataTable(String id, final IColumn<T>[] columns, ISortableDataProvider<T> dataProvider, int rowsPerPage) {
		super(id, columns, dataProvider, rowsPerPage);

		addTopToolbar(getActionToolbar());
		addTopToolbar(new HeadersToolbar(this, dataProvider));
		addBottomToolbar(new NoRecordsToolbar(this));
		addBottomToolbar(new MyNavigationToolbar(this));

	}

	public abstract ActionToolbar getActionToolbar();

	@Override
	protected Item<T> newRowItem(String id, final int index, final IModel<T> model) {

		final OddEvenItem<T> row = new OddEvenItem<T>(id, index, model);
		row.setOutputMarkupId(true);
		row.add(new AjaxEventBehavior("onclick") {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onEvent(AjaxRequestTarget target) {
				if (getSelectedRow() == null) {

					String value = (index % 2 == 0) ? "even selected" : "odd selected";
					row.add(new SimpleAttributeModifier("class", value));
					target.addComponent(row);
					setSelectedRow(row);

				} else {
					if (index != getSelectedRow().getIndex()) {

						String value = (index % 2 == 0) ? "even selected" : "odd selected";
						row.add(new SimpleAttributeModifier("class", value));
						target.addComponent(row);

						value = (getSelectedRow().getIndex() % 2 == 0) ? "even" : "odd";
						getSelectedRow().add(new SimpleAttributeModifier("class", value));
						target.addComponent(getSelectedRow());
						setSelectedRow(row);
					}

				}

			}

		});

		return row;
	}

	public void setSelectedRow(OddEvenItem<T> selectedRow) {
		this.selectedRow = selectedRow;
	}

	public OddEvenItem<T> getSelectedRow() {
		return selectedRow;
	}
}
